package com.zhihu_lvliangi_android.option.zhengwu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnCloseListener;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.file.cache.CacheUtil;
import com.android.volley.Cache;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.JsonPageModel;
import com.zhihu_lvliangi_android.domain.ZhengwuColumnItem;
import com.zhihu_lvliangi_android.option.general.GeneralTabSlidingFragment;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.zhengwu.fragment.Catergory_Theme_Fragment;
import com.zhihu_lvliangi_android.option.zhengwu.fragment.Category_Department;
import com.zhihu_lvliangi_android.service.DataFormat;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.service.TaskID;
import com.zhihu_lvliangi_android.util.Constant;
import com.zhihu_lvliangi_android.util.RequestURL;

public class AffairGuidActivity extends ParentActivity {

	private SearchView mSearchView;
	private String mCurrentSearchTip;
	private Handler handler = new MyHandler();
	private ScheduledExecutorService mScheduledExecutorService =Executors.newSingleThreadScheduledExecutor();
	private View contentView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}

	private void initView() {
		List<ParentFragment> fragments = new ArrayList<ParentFragment>();
		fragments.add(Catergory_Theme_Fragment.newInstance());
		fragments.add(Category_Department.newInstance());

		List<String> tabTitles  = new ArrayList<String>();
		tabTitles.addAll(Arrays.asList(getResources().getStringArray(R.array.zhengwu_catergory)));
		getSupportFragmentManager().beginTransaction()
				.add(R.id.container, GeneralTabSlidingFragment.newInsatnce(fragments,tabTitles)).commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater(); 
	    inflater.inflate(R.menu.affair_guid,menu); 
	    // 获取SearchView对象 
	  
	    MenuItem searchItem = menu.findItem(R.id.menu_search);
	    searchViewInit(searchItem);
		return true;
	}
	 private void searchViewInit(final MenuItem searchItem) {
		  mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
		  if(mSearchView == null){  
		        Log.e(MobileApplication.TAG,"Fail to get Search View."); 
		    }
		  mSearchView.setBaselineAligned(false);
		  // 获取搜索服务管理器  
		  	SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//		    SearchableInfo info = searchManager.getSearchableInfo(new ComponentName(this,SearchResultActivity.class)); 
//		    if(info == null){ 
//		    	showShortToast("Fail to get search info");
//		    }      
//		mSearchView.setSearchableInfo(info);
		mSearchView.setSubmitButtonEnabled(true);
		mSearchView.setIconified(false);// 设置
		mSearchView.clearFocus();// 清除焦点
		mSearchView.setQueryHint(Html.fromHtml("<font color =#FFFFFF>" + getResources().getString(R.string.search_hint) + "</font>"));
		   //设置搜索事件的监控
        mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
                
                @Override
                public boolean onQueryTextSubmit(String query) {
                        //当点击搜索按钮,输入法搜索按钮,会触发这个方法.在这里做相应的搜索事件,query为用户输入的值
                        //当输入框为空或者""时,此方法没有被调用
                		showShortToast("query =" +query);
                        return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                        //当输入的文字发生变化的时候,会触发这个方法.在这里做匹配提示的操作等
                	  if (newText != null && newText.length() > 0) {
                          mCurrentSearchTip = newText;
                          showSearchTip(newText);
                      }
                        return true;
                }
        });
        mSearchView.setOnCloseListener(new OnCloseListener() {
			
			@Override
			public boolean onClose() {
				return true;
			}
		});
	}
	 
	 private  void showSearchTip(String newText) {
	        schedule(new SearchTipThread(newText), 500);
	    }
	 private ScheduledFuture<?> schedule(Runnable command, long delayTimeMills) {
	        return mScheduledExecutorService.schedule(command,  500, TimeUnit.MILLISECONDS);	
	    }
	 
	 class SearchTipThread implements Runnable {

	        String newText;

	        public SearchTipThread(String newText){
	            this.newText = newText;
	        }

	        public void run() {
	            // keep only one thread to load current search tip, u can get data from network here
	            if (newText != null && newText.equals(mCurrentSearchTip)) {
	            	handler.sendMessage(handler.obtainMessage(1," search tip:"+ newText));
	            }
	        }
	    } 
	 
	 @SuppressLint("HandlerLeak")
	private class MyHandler extends Handler {
		 
	        @Override
	        public void handleMessage(Message msg) {
	        	
	            switch (msg.what) {
	                case 1:
	                   showShortToast(msg.obj.toString());
	                    break;
	            }
	        }
	    }
	 

	@Override
	    public boolean onPrepareOptionsMenu(Menu menu) {
	        return super.onPrepareOptionsMenu(menu);
	    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
		case R.id.menu_search:
			Log.d(MobileApplication.TAG, "menu_search");
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	

	@Override
	protected int getLayoutId() {
		return R.layout.activity_affair_guid;
	}

	@Override
	protected void setupViews() {
		initActionBar();
		initView();
	}

	@Override
	protected void initialized() {
		
	}

	@Override
	protected void threadTask() {
		
	}
	
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.homepage_actionbar_bg)));
		// 使自定义的普通View能在title栏显示, actionBar.setCustomView能起作用.
		actionBar.setIcon(new BitmapDrawable());
		actionBar.setDisplayHomeAsUpEnabled(true);//显示返回按钮
	}
	
	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		super.onResponse(response, reqId);
	}
	
	
	
}
